/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.poker;

import extension.game.poker.handler.BettingActionHandler;
import extension.game.poker.handler.SitOutHandler;
import extension.game.poker.handler.StandUpHandler;
import extension.game.poker.handler.RoomInfoHandler;
import extension.game.poker.handler.GetListPlayerHandler;
import extension.game.poker.handler.JoinGameHandler;
import extension.game.poker.handler.OnUserGoneHandler;
import com.smartfoxserver.v2.SmartFoxServer;
import log.Debug;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.extensions.SFSExtension;
import extension.game.database.Database;
import extension.game.poker.handler.ShopHandler;
import extension.game.poker.shop.Shop;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author daz 
 */
public class PokerRoomExtension extends SFSExtension {

    private PokerGame pokerGame;
    private RoomResponse response;
    ScheduledFuture<?> taskHandle;
    Database database;
    private Shop shop;

    @Override
    public void init() {
        Debug.setExt(this);
        database = new Database(getParentZone().getDBManager());
        // REGISTER REQUEST HANDLERS
        Room room = getParentRoom();
        if (room.getName().equals("The Lobby")) {
        } else {
            int roomId = Integer.parseInt(room.getName().substring(1));
            initGame(roomId);
            addRequestHandler(Command.ROOM_INFO, RoomInfoHandler.class);
            addRequestHandler(Command.JOIN_GAME, JoinGameHandler.class);
            addRequestHandler(Command.BETTING_ACTION, BettingActionHandler.class);
            addRequestHandler(Command.LIST_PLAYERS, GetListPlayerHandler.class);
            addRequestHandler(Command.STAND_UP, StandUpHandler.class);
            addRequestHandler(Command.SIT_OUT, SitOutHandler.class);
            addRequestHandler(Command.SHOP, ShopHandler.class);
            // REGISTER EVENT HANDLERS
            addEventHandler(SFSEventType.USER_DISCONNECT, OnUserGoneHandler.class);
            addEventHandler(SFSEventType.USER_LEAVE_ROOM, OnUserGoneHandler.class);
        }
        Debug.d("room addres:" + this.toString());
        if (Debug.isDebug) {
        }

    }

    private void initGame(int roomId) {
        response = new RoomResponse(this);
        pokerGame = new PokerGame(this, roomId);
        pokerGame.start();
        SmartFoxServer sfs = SmartFoxServer.getInstance();
        // Schedule the task to run every second, with no initial delay
        if (Debug.isDebug) {
            taskHandle = sfs.getTaskScheduler().scheduleAtFixedRate(pokerGame, 0, 5, TimeUnit.SECONDS);
        } else {
            taskHandle = sfs.getTaskScheduler().scheduleAtFixedRate(pokerGame, 0, 1, TimeUnit.SECONDS);
        }
    }

    public ScheduledFuture<?> getTaskHandle() {
        return taskHandle;
    }

    public RoomResponse getResponse() {
        return response;
    }

    /* GETTERS & SETTERS */
    public PokerGame getPokerGame() {
        return pokerGame;
    }

    public Shop getShop() {
        if (shop == null) {
            shop = new Shop(getParentZone());
        }
        return shop;
    }

    public void sendListPlayer(User user) {
        List<Long> listPlayerMoneys = new LinkedList<Long>();
        List<Long> listMoneyBet = new LinkedList<Long>();
        List<String> listPlayerNames = new LinkedList<String>();
        List<Integer> listPlayerPositions = new LinkedList<Integer>();
        PokerPlayer[] listPlayers = pokerGame.getListPlayers();
        for (int i = 0; i < listPlayers.length; i++) {
            if (listPlayers[i] != null) {
                if (listPlayers[i].getState() != PokerPlayer.PLAYING) {
                    listPlayerPositions.add(0);
                } else {
                    listPlayerPositions.add(1);
                }
                listPlayerNames.add(listPlayers[i].getName());
                listPlayerMoneys.add(listPlayers[i].getMoney());
                listMoneyBet.add(listPlayers[i].getBet());
            } else {
                listPlayerPositions.add(-1);
                listPlayerNames.add("0");
                listPlayerMoneys.add(0l);
                listMoneyBet.add(0l);
            }
        }
        response.sendListPlayer(listPlayerPositions, listPlayerNames, listPlayerMoneys, listMoneyBet, user);
    }

    public void sendTableInfo(User user) {
        List<String> listPlayerNames = new LinkedList<String>();
        List<Long> listPlayerMoneys = new LinkedList<Long>();
        List<Long> listMoneyBet = new LinkedList<Long>();
        List<Integer> listPlayerPositions = new LinkedList<Integer>();
        PokerPlayer[] listPlayers = pokerGame.getListPlayers();
        for (int i = 0; i < listPlayers.length; i++) {
            if (listPlayers[i] != null) {
                if (listPlayers[i].getState() != PokerPlayer.PLAYING) {
                    listPlayerPositions.add(0);
                } else {
                    listPlayerPositions.add(1);
                }
                listPlayerNames.add(listPlayers[i].getName());
                listPlayerMoneys.add(listPlayers[i].getMoney());
                listMoneyBet.add(listPlayers[i].getBet());
            } else {
                listPlayerPositions.add(-1);
                listPlayerNames.add("0");
                listPlayerMoneys.add(0l);
                listMoneyBet.add(0l);
            }
        }
        response.sendTableInfo(getParentRoom().getName(), listPlayerPositions, listPlayerNames, listPlayerMoneys, listMoneyBet, user);
    }

    public int getNumberPlayer() {
        return pokerGame.getNumberPlayers();
    }

    public Database getDatabase() {
        return database;
    }
}
